package com.mz.auth.web.controller;

import com.mz.auth.entity.DicType;
import com.mz.auth.query.BaseQuery;
import com.mz.auth.service.DicService;
import com.mz.auth.util.MzResult;
import com.mz.auth.util.PageList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("/dic")
public class DicController {

    @Autowired
    DicService dicService;

    /**
     * 跳转到信息维护页面
     */
    @GetMapping("/index")
    public String index() {
        log.info("跳转到信息维护页面");
        return "views/dic/dic_list";
    }

    /**
     * 分页查询字典类型对应数据
     */
    @GetMapping("/listpage")
    @ResponseBody
    public PageList listpage(BaseQuery baseQuery){
        log.info("查询类型数据列表");
        PageList listpage = dicService.listpage(baseQuery);
        return listpage;
    }

    /**
     * 查询所有字典类型
     */
    @RequestMapping("/queryDicType")
    @ResponseBody
    public List<DicType> queryDicType() {
        log.info("查询所有字典类型，组成树状图");
        List<DicType> dicTypes = dicService.queryDicType();
        return dicTypes;
    }

    /**
     * 按字典类型查找对应字典类型数据的ids
     */
    @PostMapping("/queryDicTypeDataIdsByTypeid")
    @ResponseBody
    public List<Integer> queryDicTypeDataIdsByTypeid(Long typeid){
        log.warn("[功能未实现]查询typeid={}的所有字典数据的id",typeid);
        return null;
    }

    /*数据字典增删改 与 查单个信息回显*/
    /**
     * 根据数据字典id查询其信息
     */
    @PostMapping("/queryDicTypeInfoById")
    @ResponseBody
    public DicType queryDicTypeInfoById(Long id){
        log.info("根据数据字典id={}查询其信息",id);
        DicType dicType = dicService.queryDicTypeInfoById(id);
        return dicType;
    }

    /**
     * 新增数据字典
     */
    @PostMapping("/saveDicType")
    @ResponseBody
    public MzResult saveDicType(@RequestBody DicType dicType){
        log.info("根据数据字典{}",dicType);
        try{
            dicService.saveDicType(dicType);
            return MzResult.ok();
        }catch (Exception exception){
            exception.printStackTrace();
            return MzResult.error(exception.getMessage());
        }
    }

    /**
     * 修改数据字典
     */
    @PostMapping("/editSaveDicType")
    @ResponseBody
    public MzResult editSaveDicType(@RequestBody DicType dicType){
        log.info("修改数据字典{}",dicType);
        try{
            dicService.editSaveDicType(dicType);
            return MzResult.ok();
        }catch (Exception exception){
            exception.printStackTrace();
            return MzResult.error(exception.getMessage());
        }
    }

    /**
     * 删除数据字典(需要多表删除)
     */
    @PostMapping("/deleteDicType")
    @ResponseBody
    public MzResult deleteDicType(Long id){
        log.info("[功能未实现]删除数据字典与其类型数据,id={}",id);
        return MzResult.error("功能未实现");
    }


}
